package com.zenith.zongheng.demo.controller;

import com.zenith.zongheng.auth.api.AuthClientService;
import com.zenith.zongheng.auth.api.AuthService;
import com.zenith.zongheng.core.result.R;
import com.zenith.zongheng.core.util.StringUtil;
import com.zenith.zongheng.demo.api.LeaveInfoService;
import com.zenith.zongheng.system.domain.vo.SysRoleDetailVO;
import com.zenith.zongheng.system.domain.vo.SysUserinfoVO;
import com.zenith.zongheng.system.service.SysRoleServiceImpl;
import com.zenith.zongheng.system.service.SysUserinfoServiceImpl;
import com.zenith.zongheng.system.service.SysUserroleServiceImpl;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hzp
 * @since 2021-12-29
 */
@RestController
@RequestMapping({"/userInfo"})
public class UserController {

    @Autowired
    @Reference
    private AuthService authService;
    @Autowired
    @Reference
    private AuthClientService authClientService;

    @Autowired
    @Reference
    private LeaveInfoService leaveInfoService;

    @Autowired
    @Reference
    SysUserinfoServiceImpl sysUserinfoServiceImpl;


    @Autowired
    @Reference
    SysUserroleServiceImpl sysUserroleServiceImpl;

    @Autowired
    @Reference
    SysRoleServiceImpl sysRoleServiceImpl;


    /**
     * 获得当前登陆用户的信息 （职务，申请人姓名，申请单位）
     *
     * @author hzp
     * @since 2021-12-31
     */
    @PostMapping({"/getUser"})
    public R getUser(HttpServletRequest request)
    {
        String token = StringUtil.defaultString(request.getHeader("token"));
        System.err.println(this.authClientService.getUserSession(token));
        return R.ok(this.authClientService.getUserSession(token));
    }


    /**
     * 获得当前登陆用户的职务 （民警，领导干部，区县领导，市局）
     *
     *      职务："name": "市局班子",
     * @author hzp
     * @since 2021-12-31
     */
    @PostMapping({"/getUserRole"})
    public R getUserRole(HttpServletRequest request)
    {
        String token = StringUtil.defaultString(request.getHeader("token"));
        //获得当前登陆的对象信息的（UnitUserId）从而获得当前用户的角色
        System.err.println(this.authClientService.getUserSession(token).getUnitUserId());
        Long unitUserId = this.authClientService.getUserSession(token).getUnitUserId();
        Long[] roleIdsByUnituserId = sysUserroleServiceImpl.getRoleIdsByUnituserId(unitUserId);
        SysRoleDetailVO detailById = sysRoleServiceImpl.getDetailById(roleIdsByUnituserId[0]);
        System.err.println(detailById);
        return R.ok(detailById);
    }


    /**
     * 获得当前登陆用户的信息（性别，年龄）
     *
     *
     * @author hzp
     * @since 2021-12-31
     */

    @PostMapping({"/getUserInfo"})
    public R getUserInfo(HttpServletRequest request)
    {
        //获得当前登陆的对象信息的（UnitUserId）从而获得当前用户的角色
        String token = StringUtil.defaultString(request.getHeader("token"));
        System.err.println(this.authClientService.getUserSession(token).getId());
        //获取当前登陆用户的id
        Long id = this.authClientService.getUserSession(token).getId();
        //通过当前登陆的用户id查询userInfo表
        SysUserinfoVO userVOById = sysUserinfoServiceImpl.getUserVOById(id);
        return R.ok(userVOById);
    }


}
